package com.zmj.sy.mom.srv.aps.controller;

import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.plan.*;
import com.zmj.sy.mom.srv.aps.service.PlanMbdService;
import com.zmj.sy.mom.srv.aps.service.PlanService;
import com.zmj.sy.mom.srv.aps.utils.ApsConstant;
import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Api(tags = "计划")
@RestController
@RequestMapping("/plan")
public class PlanController {

    private final PlanService planService;
    private final StringRedisTemplate stringRedisTemplate;
    private final PlanMbdService planMbdService;

    @ApiOperation("高级排产-纯倒排")
    @PostMapping("/scheduling")
    public BaseResVo scheduling(@RequestBody PlanSchedulingReqVo reqVo){

        // 检查redis是否有锁定状态，如果有则提示，并且不排产
        ValueOperations<String, String> valOperation = stringRedisTemplate.opsForValue();
        String lockFlag = valOperation.get(ApsConstant.APS_SCHEDULING_LOCK);
        if (StringUtils.hasText(lockFlag)) {
            throw SyExceptionUtils.e("{} 等订单排产中!", lockFlag);
        }

        try {
            if(reqVo.getSchedulingRule() == null) {
                reqVo.setSchedulingRule(2);
            }
            planService.scheduling(reqVo);
        } finally {
            planService.unlock(reqVo);
        }
        return BaseResVo.ok();
    }

    @ApiOperation("预排产-前拉后推")
    @PostMapping("/schedulingMbd")
    public BaseResVo schedulingMbd(@RequestBody PlanSchedulingReqVo reqVo){

        // 检查redis是否有锁定状态，如果有则提示，并且不排产
        ValueOperations<String, String> valOperation = stringRedisTemplate.opsForValue();
        String lockFlag = valOperation.get(ApsConstant.APS_SCHEDULING_LOCK);
        if (StringUtils.hasText(lockFlag)) {
            throw SyExceptionUtils.e("{} 等订单排产中!", lockFlag);
        }

        try {
            if(reqVo.getSchedulingRule() == null) {
                reqVo.setSchedulingRule(3);
            }
            planMbdService.scheduling(reqVo);
        } finally {
            planMbdService.unlock(reqVo);
        }

        return BaseResVo.ok();
    }

    @ApiOperation("高级排产-纯倒排重排")
    @PostMapping("/reScheduling")
    public BaseResVo reScheduling(@RequestBody PlanReSchedulingReqVo reqVo){

        // 检查redis是否有锁定状态，如果有则提示，并且不排产
        ValueOperations<String, String> valOperation = stringRedisTemplate.opsForValue();
        String lockFlag = valOperation.get(ApsConstant.APS_SCHEDULING_LOCK);
        if (StringUtils.hasText(lockFlag)) {
            throw SyExceptionUtils.e("{} 等订单排产中!", lockFlag);
        }

        try {
            if(reqVo.getSchedulingRule() == null){
                reqVo.setSchedulingRule(2);
            }
            planService.reScheduling(reqVo);
        } finally {
            planService.unlock(reqVo);
        }
        return BaseResVo.ok();
    }

    @ApiOperation("月度计划")
    @GetMapping("/monthly")
    public BaseListResVo<PlanMonthlyResVo> monthly(PlanMonthlyReqVo reqVo) {
        return new BaseListResVo<>(planService.monthly(reqVo));
    }

    @ApiOperation("查询部件类型列表")
    @GetMapping("/partTypes")
    public BaseListResVo<Map<String, Object>> getPartTypes() {
        List<Map<String, Object>> partTypes = new ArrayList<>();
        partTypes.add(createPartTypeMap("顶梁", 1));
        partTypes.add(createPartTypeMap("掩护梁", 2));
        partTypes.add(createPartTypeMap("底座", 3));
        partTypes.add(createPartTypeMap("侧板", 4));
        partTypes.add(createPartTypeMap("连杆", 5));
        return new BaseListResVo<>(partTypes);
    }

    private Map<String, Object> createPartTypeMap(String name, int value) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("value", value);
        return map;
    }

    @ApiOperation("根据订单类型查询订单列表")
    @GetMapping("/mbdOrder")
    public BaseListResVo<PlanMbdOrderResVo> mbdOrder(PlanMbdOrderReqVo reqVo) {
        return new BaseListResVo<>(planService.mbdOrder(reqVo));
    }

    @ApiOperation("生产计划")
    @PostMapping("/pro")
    public BaseListResVo<PlanProResVo> pro(@RequestBody PlanProReqVo reqVo) {

        if(StringUtils.hasText(reqVo.getConstructCode())){
            reqVo.setConstructCode(reqVo.getConstructCode().toUpperCase().trim());
        }

        return planService.pro(reqVo);
    }


}
